﻿<?php 



echo '<pre>';
	
	// $data = json_decode(getDateAll(2));
	// var_dump($data->data);
	// modifyData();

	echo aggregate();
	
echo '</pre>';

function getDateAllCount($userLevel){



	$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

	$filter=["userLevel"=>(int)$userLevel];  //条件
	$options=[]; //排序和其它
	// $filter=[];
	// $options=[];
	$query=new MongoDB\Driver\Query($filter,$options);

	$cursor = $manager->executeQuery('runoobdb.byrgPhone',$query);
	$arr = array(); //将所有的数据先取出来
	foreach($cursor as $document){
		array_push($arr,(array)$document);

	}

	return count($arr);

}


function aggregate(){ //按等级分组查询

	$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
	$param = [
	    'aggregate' => 'byrgPhone',
	    'pipeline' => [
	        ['$match'=>['createdAt'=>[$gt=>"2021-03-10 00:00:00",$lt=>"2021-03-11 23:59:59"] ]],
	        ['$group' => ['_id' => ['userLevel'=>'$userLevel','followerName'=>'$followerName','createdAt'=>['$substr'=>array('$createdAt',0,10)]  ]
	        			,'hr_count' => ['$sum' => 1]]]
	    ],
	    'cursor'=>new stdClass,

	];
	$cmd = new MongoDB\Driver\Command($param);
	$cursor=$manager->executeCommand('runoobdb', $cmd);
	$arr=array();
	foreach ($cursor as $key => $document) {
		
		echo '<pre>';

			// var_dump($document);

			// var_dump($document->hr_count);

			// $document->_id->hr_count = $document->hr_count;

			array_push($arr,$document);

		echo '</pre>';

	}

	echo '<pre>';

		var_dump($arr);

	echo '</pre>';

	// return json_encode(

	// 	[	'code'=>0,
	// 		"msg"=>"获取数据成功",
	// 		'count'=>count($arr),
	// 		'data'=>$arr
	// 	],
	// 	JSON_UNESCAPED_UNICODE
	// );
}



function getDateAll($userLevel){  //获取全部A客数据

	$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

	$filter=["userLevel"=>(int)$userLevel];  //条件
	$options=["sort"=>["mobile"=>1],"limit"=>20,"skip"=>1]; //排序和其它
	// $filter=[];
	// $options=[];
	$query=new MongoDB\Driver\Query($filter,$options);

	$cursor = $manager->executeQuery('runoobdb.byrgPhone',$query);
	$arr = array(); //将所有的数据先取出来
	foreach($cursor as $document){
		array_push($arr,(array)$document);

	}

	return json_encode(

		[	'code'=>0,
			"msg"=>"获取数据成功",
			'count'=>count($arr),
			'data'=>$arr
		],
		JSON_UNESCAPED_UNICODE
	);
}

	

function modifyData(){


	//链接mongodb
	$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
	$bulk = new MongoDB\Driver\BulkWrite; //默认是有序的，串行执行
	//$bulk = new MongoDB\Driver\BulkWrite(['ordered' => flase]);//如果要改成无序操作则加flase，并行执行

	//$set相当于mysql的 set，这里和mysql有两个不同的地方，
	//1：字段不存在会添加一个字段;
	//2：mongodb默认如果条件不成立，新增加数据，相当于insert
	 
	 
	//如果条件不存在不新增加，可以通过设置upsert
	//db.collectionName.update(query, obj, upsert, multi);
	 
	$bulk->update(

		['_id' => '2971693129'],
		[
			'$set'=>['avatarUrl'=>'张献伟']
		],
		['multi' => true, 'upsert' => false] 
		//multi为true,则满足条件的全部修改,默认为true，如果改为false，则只修改满足条件的第一条
		//upsert为 treu：表示不存在就新增
	);

	$manager->executeBulkWrite('runoobdb.byrgPhone', $bulk); //执行写入 location数据库下的box集合

}


?>